<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>Hello APP</title>
    <link rel="stylesheet" type="text/css" href="./css/api.css" />
    <style type="text/css">
        html,
        body {
            height: 100%;
        }
    </style>
</head>

<body>

</body>
<script type="text/javascript" src="./script/api.js"></script>
<script type="text/javascript">
    apiready = function() {
//        checkUpdate();
        init();
        fnGetWareTypeList();
        api.openWin({
            name: 'main',
            url: './html/main.html',
            sildBackEnabled: false //兼容ios
        });

    };

    function checkUpdate() {
        var mam = api.require('mam');
        mam.checkUpdate(function(ret, err) {
            if (ret) {
                var result = ret.result;
                if (result.update == true && result.closed == false) {
                    var str = '版本号:' + result.version + ';更新内容:' + result.updateTip + ';发布时间:' + result.time;
                    api.confirm({
                        title : '有新的版本,是否下载并安装',
                        msg : str,
                        buttons : ['确定', '取消']
                    }, function(ret, err) {
                        if (ret.buttonIndex == 1) {
                            if (api.systemType == "android") {
                                api.download({
                                    url : result.source,
                                    report : true
                                }, function(ret, err) {
                                    if (ret && 0 == ret.state) {/* 下载进度 */
                                        api.toast({
                                            msg : "正在下载应用" + ret.percent + "%",
                                            duration : 2000
                                        });
                                    }
                                    if (ret && 1 == ret.state) {/* 下载完成 */
                                        var savePath = ret.savePath;
                                        api.installApp({
                                            appUri : savePath
                                        });
                                    }
                                });
                            }
                            if (api.systemType == "ios") {
                                api.installApp({
                                    appUri : result.source
                                });
                            }
                        }
                    });
                } else {
                    api.console('暂无更新');
                }
            } else {
                api.alert({
                    msg : err.msg
                });
            }
        });
    }

    function init() {
        var wareTypeList = $api.getStorage('wareTypeList');
        if (!wareTypeList) {
            wareTypeList = [{
                "id": "56c80da883af652643474b6b",
                "name": "水果",
                "banner": {
                    "url": "http://aae9d227f777909e5899.qiniucdn.apicloud-system.com/apicloud/f761997f8fd33af25af5354737e37bcc.png",
                    "name": "56c80da883af652643474b6b.png",
                    "id": "57f63bdf0d3febfe58585568"
                },
                "createdAt": "2016-02-20T06:54:32.808Z",
                "updatedAt": "2016-10-06T11:56:16.288Z"
            }, {
                "id": "56c80db78d04c83d3d1dedea",
                "name": "食材",
                "banner": {
                    "url": "http://aae9d227f777909e5899.qiniucdn.apicloud-system.com/apicloud/be93f9fdeda576ad81d0c728528cef50.png",
                    "name": "56c80dc88d04c83d3d1dedf3.png",
                    "id": "57f63be70d3febfe5858556c"
                },
                "createdAt": "2016-02-20T06:54:47.996Z",
                "updatedAt": "2016-10-06T11:56:23.906Z"
            }, {
                "id": "56c80dc031da9e480de1cb49",
                "name": "零食",
                "banner": {
                    "url": "http://aae9d227f777909e5899.qiniucdn.apicloud-system.com/apicloud/be93f9fdeda576ad81d0c728528cef50.png",
                    "name": "56c80dc88d04c83d3d1dedf3.png",
                    "id": "57f63bec9592b3fc6a341b59"
                },
                "createdAt": "2016-02-20T06:54:56.243Z",
                "updatedAt": "2016-10-06T11:56:28.778Z"
            }, {
                "id": "56c80dc383af652643474b6d",
                "name": "牛奶",
                "banner": {
                    "url": "http://aae9d227f777909e5899.qiniucdn.apicloud-system.com/apicloud/f761997f8fd33af25af5354737e37bcc.png",
                    "name": "56c80da883af652643474b6b.png",
                    "id": "57f63bf0c6f05d0956108828"
                },
                "createdAt": "2016-02-20T06:54:59.891Z",
                "updatedAt": "2016-10-06T11:56:32.300Z"
            }, {
                "id": "56c80dc88d04c83d3d1dedf3",
                "name": "蔬菜",
                "banner": {
                    "url": "http://aae9d227f777909e5899.qiniucdn.apicloud-system.com/apicloud/be93f9fdeda576ad81d0c728528cef50.png",
                    "name": "56c80db78d04c83d3d1dedea.png",
                    "id": "57f63bf39592b3fc6a341b5d"
                },
                "createdAt": "2016-02-20T06:55:04.285Z",
                "updatedAt": "2016-10-06T11:56:35.789Z"
            }];
        }
    }

    function fnGetWareTypeList() {
        var params = {
            fields: {},
            where: {},
            skip: 0,
            limit: 5
        }
        params = $api.jsonToStr(params);
        api.ajax({
            url: 'http://d.apicloud.com/mcm/api/wareType?filter=' + params,
            method: 'get',
            headers: {
                "X-APICloud-AppId": "A6914327011091",
                "X-APICloud-AppKey": "8ac17d22e49cb7982d82796097cec52a6c7cd01d.1475375422841"
            }
        }, function(ret, err) {
            if (ret) {
                $api.setStorage('wareTypeList', ret);
            } else {
                console.log($api.jsonToStr(ret));
            }
        });
    }
</script>

</html>
